package com.lsh.payment.core.service.async.impl;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.lsh.payment.core.model.payasync.payTaskModel;
import com.lsh.payment.core.model.task.PayTask;
import com.lsh.payment.core.model.payment.PayDeal;
import com.lsh.payment.core.service.async.IEventListener;
import com.lsh.payment.core.service.paytask.PayTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Project Name: lsh-payment
 * @author by jingyuan
 * Date: 16/12/22
 * 北京链商电子商务有限公司
 * Package com.lsh.payment.core.service.AsyncService
 * desc:异步处理监听类
 */
@Component
public class PayTaskEventListener implements IEventListener {
    private Logger logger = LoggerFactory.getLogger(PayTaskEventListener.class);

    @Autowired
    private PayTaskService payTaskService;


    /**
     * 插入payTask表
     *
     * @param payTaskModel 监听类
     */
    @Subscribe
    @AllowConcurrentEvents
    public void initPayTask(payTaskModel payTaskModel) {
        logger.info("插入payTask表开始");
        try {
            PayTask payTask = new PayTask();
            PayDeal payDeal = payTaskModel.getPayDeal();

            payTask.setTradeId(payDeal.getTradeId());
            payTask.setPayId(payDeal.getPayId());
            payTask.setPayPaymentNo(payDeal.getPayPaymentNo());
            payTask.setChannelTransaction(payDeal.getChannelTransaction());
            payTask.setTradeModule(payDeal.getTradeModule());
            payTask.setCreatedAt(new Date());
            payTask.setUpdatedAt(new Date());
            payTask.setVenderId(payDeal.getVenderId());

            payTaskService.addPayTask(payTask);

        } catch (Exception ex) {
            logger.error(" add payTask error ", ex);
        }

        logger.info("插入payTask表结束");
    }

}
